package com.example.gfsquadwebviewproject.util;

import android.util.Log;
import com.example.gfsquadwebviewproject.data.Constant;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * TimeUtils
 *
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-8-24
 */
public class TimeUtils {

    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    public static final SimpleDateFormat DATE_FORMAT_TIME = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);

    private TimeUtils() {
        throw new AssertionError();
    }

    /**
     * long time to string
     *
     * @param timeInMillis
     * @param dateFormat
     * @return
     */
    public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
        return dateFormat.format(new Date(timeInMillis));
    }

    /**
     * long time to string, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @param timeInMillis
     * @return
     */
    public static String getTime(long timeInMillis) {
        return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
    }

    //当前系统时间
    public static String getTime() {
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = DATE_FORMAT_TIME.format(curDate);
        return str;
    }

    //当前系统时间
    public static String getDate_Time() {
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = DEFAULT_DATE_FORMAT.format(curDate);
        return str;
    }

    //当前系统时间
    public static String getDate() {
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = DATE_FORMAT_DATE.format(curDate);
        return str;
    }

    //获得秒
    public static long getDateSec(){
        Date curDate =  new Date(System.currentTimeMillis());
        return curDate.getTime()/1000;
    }

    /**
     * 根据当前日期获得是星期几
     * time=yyyy-MM-dd
     * @return
     */
    public static String getWeekDay() {

        Date date = new Date(System.currentTimeMillis());
        String Week = "";
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int wek = c.get(Calendar.DAY_OF_WEEK);

        if (wek == 1) {
            Week += "星期日";
        }
        if (wek == 2) {
            Week += "星期一";
        }
        if (wek == 3) {
            Week += "星期二";
        }
        if (wek == 4) {
            Week += "星期三";
        }
        if (wek == 5) {
            Week += "星期四";
        }
        if (wek == 6) {
            Week += "星期五";
        }
        if (wek == 7) {
            Week += "星期六";
        }
        return Week;

    }



    //保存日志时间超过聊天则返回ture，然后删除日志文件
    public static boolean timeDifference(String time){
        try {
            Date date1 = DEFAULT_DATE_FORMAT.parse(time);
            Date date2 = new Date(System.currentTimeMillis());//获取当前时间
            int times= (int)((date2.getTime() - date1.getTime())/1000);//得到秒
            if(times> Constant.saveFileTime){
                return true;
            }
        }
        catch (Exception e){
            Log.d("Context","计算时间出错:"+e);
        }
        return false;
    }


    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static long getCurrentTimeInLong() {
        return System.currentTimeMillis();
    }

    /**
     * get current time in milliseconds, format is {@link #DEFAULT_DATE_FORMAT}
     *
     * @return
     */
    public static String getCurrentTimeInString() {
        return getTime(getCurrentTimeInLong());
    }

    /**
     * get current time in milliseconds
     *
     * @return
     */
    public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
        return getTime(getCurrentTimeInLong(), dateFormat);
    }
}
